"""
聊天记录模型
"""
import datetime
from apps.extensions import db

class ChatRecord(db.Model):
    __tablename__ = 'cms_chat_record'
    
    id = db.Column(db.Integer, primary_key=True, comment='聊天记录ID')
    session_id = db.Column(db.String(64), nullable=False, comment='会话ID')
    user_id = db.Column(db.Integer, nullable=True, comment='用户ID')
    user_name = db.Column(db.String(50), nullable=True, comment='用户名称')
    user_phone = db.Column(db.String(20), nullable=True, comment='用户电话')
    message_type = db.Column(db.String(20), default='text', comment='消息类型:text,image,file,voice')
    content = db.Column(db.Text, nullable=False, comment='消息内容')
    sender_type = db.Column(db.String(20), default='user', comment='发送者类型:user,admin,system')
    ip_address = db.Column(db.String(50), nullable=True, comment='IP地址')
    user_agent = db.Column(db.String(500), nullable=True, comment='用户代理')
    status = db.Column(db.String(20), default='sent', comment='消息状态:sent,delivered,read')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')

    def __repr__(self):
        return f'<ChatRecord {self.id}>'

class ChatSession(db.Model):
    __tablename__ = 'cms_chat_session'
    
    id = db.Column(db.Integer, primary_key=True, comment='会话ID')
    session_id = db.Column(db.String(64), unique=True, nullable=False, comment='会话唯一标识')
    user_id = db.Column(db.Integer, nullable=True, comment='用户ID')
    user_name = db.Column(db.String(50), nullable=True, comment='用户名称')
    user_phone = db.Column(db.String(20), nullable=True, comment='用户电话')
    ip_address = db.Column(db.String(50), nullable=True, comment='IP地址')
    user_agent = db.Column(db.String(500), nullable=True, comment='用户代理')
    status = db.Column(db.String(20), default='active', comment='会话状态:active,closed,timeout')
    start_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='开始时间')
    end_time = db.Column(db.DateTime, nullable=True, comment='结束时间')
    message_count = db.Column(db.Integer, default=0, comment='消息数量')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')

    def __repr__(self):
        return f'<ChatSession {self.session_id}>' 